//
//  XiaoximenDateDataSource.m
//  Xiaoximen_iOS
//
//  Created by Wang Zhongyu on 12-7-17.
//  Copyright (c) 2012年 Peking University. All rights reserved.
//

#import "XiaoximenDateDataSource.h"
#import "XiaoximenTimelineViewController.h"
#import "KalPrivate.h"

@implementation XiaoximenDateDataSource
@synthesize selectedDates = _selectedDates;
@synthesize selectedPlaces = _selectedPlaces;
@synthesize partyTitle = _partyTitle;
@synthesize viewController;
@synthesize isEndLoading = _isEndLoading;
@synthesize loadingSelectNumber;

- (NSMutableArray *)selectedPlaces
{
	if (_selectedPlaces == nil) {
		_selectedPlaces = [[NSMutableArray alloc] init];
	}
	return _selectedPlaces;
}
- (NSMutableArray *)selectedDates
{
	if (_selectedDates == nil) {
		_selectedDates = [[NSMutableArray alloc] init];
	}
	return _selectedDates;
}
- (BOOL)isEndLoading
{
	if (loadingSelectNumber >= 3) return YES;
	else loadingSelectNumber ++;
	return NO;
}

+ (XiaoximenDateDataSource*)dataSource
{
	return [[[self class] alloc] init];
}

#pragma mark UITableViewDataSource protocol conformance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *identifier = @"MyCell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	
	cell.textLabel.text = [self.selectedDates objectAtIndex:indexPath.row];
	return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	return self.selectedDates.count;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
	return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
	NSUInteger row = [indexPath row];
	[self.selectedDates removeObjectAtIndex:row];
	[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
					 withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark KalDataSource protocol conformance

- (void)presentingDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate delegate:(id<KalDataSourceCallbacks>)delegate
{
	[delegate loadedDataSource:self];
}

- (NSArray *)markedDatesFrom:(NSDate *)fromDate to:(NSDate *)toDate
{
	return [NSArray array];
}

- (void)loadItemsFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
	if (!self.isEndLoading) return;
	
	XiaoximenTimelineViewController *tvc = [[XiaoximenTimelineViewController alloc] initWithNibName:@"XiaoximenTimelineViewController" bundle:nil];
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy年MM月dd日";
	NSDate *date = fromDate;
    NSString *dateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
	tvc.date = date;
	tvc.dateString  = dateString;
	tvc.parentDataSource = self;
	[self.viewController presentModalViewController:tvc animated:YES];
}

- (void)addDate:(NSString *)dateStr Times:(NSArray *)timeArr
{
	for (NSString *time in timeArr) {
		[self.selectedDates addObject:[NSString stringWithFormat:@"%@ %@", dateStr, time]];
	}
	[self.viewController reloadData];
}

- (void)removeAllItems
{
	// do nothing
}

@end
